﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Conmmon
{
    public class PageList<T> : List<T>
    {
        public int CurrentPage { get; set; }
        public int TotalPages { get; set; }
        public int PageSize { get; set; }
        public int TotalCount { get; set; }
        public bool HasPrevious => CurrentPage > 1;
        public bool HasNext => CurrentPage < TotalPages;

        public PageList(List<T> items, int count, int pageNumber, int pageSize)
        {

            PageSize = pageSize;
            TotalCount = count;
            CurrentPage = pageNumber;
            TotalPages = (int)Math.Ceiling(count / (double)pageSize);
            AddRange(items);
        }
        public static async Task<PageList<T>> CreateAsync(IQueryable<T> source, int current, int pageSize)
        {
            var count = await source.CountAsync();

            List<T> items = new List<T>();
            if (count > 0)
            {
                items = await source.Skip(pageSize * (current - 1)).Take(pageSize).ToListAsync();
            }
            return new PageList<T>(items, count, current, pageSize);
        }

        public static async Task<PageList<T>> CreateAsync(IQueryable<T> source,BaseParameter Params)
        {
            var count = await source.CountAsync();

            List<T> items = new List<T>();
            if (count > 0)
            {
                items = await source.Skip(Params.PageSize * (Params.Current - 1)).Take(Params.PageSize).ToListAsync();
            }
            return new PageList<T>(items, count, Params.Current,Params.PageSize);
        }
    }
}
